package com.caseProcess.controller;

import com.alibaba.fastjson.JSONObject;
import com.caseProcess.entity.CaseProcInfo;
import com.caseProcess.entity.Result;
import com.caseProcess.entity.VCaseProcInfoByPolice;
import com.caseProcess.service.CaseProcInfoService;
import com.caseProcess.service.VCaseProcInfoByPoliceService;
import com.caseProcess.utils.IdWorker;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ahuo
 * @version 1.0
 * @date 2020年11月15日 15点15分
 */
@RestController
@RequestMapping("/caseProcInfo")
public class CaseProcInfoController {

    @Resource
    CaseProcInfoService caseProcInfoService;

    @Resource
    VCaseProcInfoByPoliceService vCaseProcInfoByPoliceService;

    IdWorker idWorker=new IdWorker(1,1,1);

    /**
     * askforok详细页用到
     * 查询分派信息
     * @param caseProcInfoJson
     * @return 操作结果
     */
    @RequestMapping("/select")
    public Result selectCPI(String caseProcInfoJson){
        CaseProcInfo caseProcInfo = JSONObject.parseObject(caseProcInfoJson, CaseProcInfo.class);
        CaseProcInfo caseProcInfoList = caseProcInfoService.getCaseProcInfos(caseProcInfo.getProcessId());
        Result result = new Result();
        if (caseProcInfoList != null){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(caseProcInfoList);
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }

    /**
     * 指，领办结详情页
     * 查询分派信息
     * @param processId
     * @return 操作结果
     */
    @RequestMapping("/selectCaseProcInfo")
    public Result selectCaseProcInfo(String parentId, String processId){
        VCaseProcInfoByPolice vCaseProcInfoByPolice = vCaseProcInfoByPoliceService.selectCaseProcInfo(parentId,processId);
        Result result = new Result();
        if (vCaseProcInfoByPolice != null){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
            result.setData(vCaseProcInfoByPolice);
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }

    /**
     * 添加分派信息
     * @param caseProcInfoJson 分派信息Json
     * @return 操作结果
     */
    @RequestMapping("/insert")
    public Result insertCPI(String caseProcInfoJson) {
        CaseProcInfo caseProcInfo = JSONObject.parseObject(caseProcInfoJson, CaseProcInfo.class);
        caseProcInfo.setProcessId(idWorker.nextId()+"");
        boolean  isSuccess = false;
        try {
           isSuccess = caseProcInfoService.insert(caseProcInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Result result = new Result();
        if (isSuccess){
            result.setCode(Result.RESULT_OK);
            result.setMsg("操作成功");
        }else{
            result.setCode(Result.RESULT_ERROR);
            result.setMsg("操作失败");
        }
        return result;
    }



}
